﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）

//【头文件包含】
#include"Queue.h"
#include"BinaryTree.h"

//【函数定义】


/*队列初始化*/void QueueInit(Que* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}
/*队列空间销毁*/void QueueDestroy(Que* pq)
{
	assert(pq);
	QN* cur = pq->head;
	while (cur)
	{
		QN* next = cur->next;
		free(cur);
		cur = next;
	}
}
/*队列数据插入*/void QueuePush(Que* pq, QNDataType x)
{
	assert(pq);

	QN* newnode = (QN*)malloc(sizeof(QN));
	newnode->data = x;
	newnode->next = NULL;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
		
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
/*队列数据删除*/void QueuePop(Que* pq)
{
	assert(pq);
	assert(pq->head!=NULL);
	QN* newhead = pq->head->next;
	free(pq->head);
	pq->head = newhead;
	if (pq->head == NULL)pq->tail = NULL;
}
/*队列队头数据提取*/QNDataType QueueFront(Que* pq)
{
	assert(pq);
	assert(pq->head != NULL);
	return pq->head->data;

}
/*队列队尾数据提取*/QNDataType QueueBack(Que* pq)
{
	assert(pq);
	assert(pq->head != NULL);
	return pq->tail->data;
}
/*队列大小计算*/int QueueSize(Que* pq)
{
	assert(pq);
	int size = 0;
	QN* cur = pq->head;
	while (cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}
/*队列空判断*/bool QueueEmpty(Que* pq)
{
	assert(pq);
	return pq->head == NULL;
}
/*队列打印*/void QueuePrint(Que* pq)
{
	assert(pq);

	QN* cur = pq->head;
	printf("HEAD| ");

	while(cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL |TAIL\n");
}
/*队列反向打印*/void QueuePrintRev(Que* pq)
{
	assert(pq);

	QN* cur = pq->head;
	QN* curt = pq->tail;
	printf("TAIL| NULL");

	while (curt!=cur)
	{
		while (cur->next != curt)
		{
			cur = cur->next;
		}
		printf("<-%d", cur->next->data);
		curt = cur;
		cur = pq->head;
	}
	printf("<-%d", cur->data);
	printf(" |HEAD\n");
}
/**/
/**/